package com.vgmobile.cucrever2.home.category.child;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.vgmobile.cucrever2.R;
import com.vgmobile.cucrever2.ResideMenuActivity;
import com.vgmobile.cucrever2.api.ItemListHoz;
import com.vgmobile.cucrever2.global.BaseBackPressedListener;
import com.vgmobile.cucrever2.product.list.ProductListActivity;

public class CategoryChildFragment extends Fragment{
	
	private ResideMenuActivity activity;
	private View view;
	private ListView listView;
	private ListCategoryChildAdapter listCategoryChildAdapter;
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if(view == null){
			activity = (ResideMenuActivity) getActivity();
			view = inflater.inflate(R.layout.category_child_layout, container, false);
			listView = (ListView) view.findViewById(R.id.category_child_list);
		}else{
			((ViewGroup)view.getParent()).removeView(view);
		}
		
		ArrayList<ItemListHoz> listItemEvent = new ArrayList<ItemListHoz>();
        for (int i = 0; i < 5; i++) {
        	ItemListHoz item = new ItemListHoz();
			if (i%2 == 0) {
				item.setLinkImage("https://dl.dropboxusercontent.com/u/71228229/TestCucre/event_1.png");
			}else{
				item.setLinkImage("https://dl.dropboxusercontent.com/u/71228229/TestCucre/event_2.png");
			}
			item.setTitle("World Class " + i);
			listItemEvent.add(item);
		}
        
        ArrayList<ItemListHoz> listItemCat = new ArrayList<ItemListHoz>();
        for (int i = 0; i < 12; i++) {
        	ItemListHoz item = new ItemListHoz();
        	item.setTitle("Category con thứ  " + i);
        	item.setLinkImage("http://www.bongda.com.vn/Data/Image/2014/Thang05/14/Gasson4.jpg");
        	listItemCat.add(item);
		}
        listCategoryChildAdapter = new ListCategoryChildAdapter(activity, listItemCat);
        
        listView.setAdapter(listCategoryChildAdapter);
        activity.setTitleMenuHome("Danh mục sản phẩm con");
        
		activity.setOnBackPressedListener(new BaseBackPressedListener(getFragmentManager()));
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> adapterView, View view, int postion, long id) {
				Intent intent = new Intent(activity, ProductListActivity.class);
				activity.startActivity(intent);
			    activity.overridePendingTransition(R.anim.activity_fade_in, R.anim.activity_fade_out);
			}
		});
	}
	
	@Override
	public void onDestroyView() {
//		listCategoryChildAdapter.clearMem();
//		unbindDrawables(view);
//		System.gc();
		super.onDestroyView();
	}
    
	private void unbindDrawables(View view) {
		try{
	        if (view.getBackground() != null) {
	        	view.getBackground().setCallback(null);
	        }
	        if (view instanceof ViewGroup) {
	            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
	            	unbindDrawables(((ViewGroup) view).getChildAt(i));
	            }
	            try{
	            	((ViewGroup) view).removeAllViews();
	            }catch (Exception e) {
					// TODO: handle exception
				}
	        }
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
}
